package utils

import (
	"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
	"strings"
)

func FilterKeysContainsKeyword(keys []string, keyword string) []string {
	var result []string
	for _, key := range keys {
		if strings.Contains(key, keyword) {
			result = append(result, key)
		}
	}
	return result
}

func FilterKeysContainsWithoutPrefix(keys []string, keyword, prefix string) []string {
	var result []string
	for _, key := range keys {
		_, k, _ := strings.Cut(key, prefix)
		if k != "" {
			k = k[1:]
		}
		index := strings.Index(k, ":")
		if index >= 0 {
			k = k[index:]
		}

		if strings.Contains(k, keyword) {
			result = append(result, key)
		}
	}
	return result
}

func UnionKeysFilter(keys1 ...[]string) ([]string, errors.EdgeX) {
	keys := make(map[string]string)
	for _, ks := range keys1 {
		for _, k := range ks {
			keys[k] = k
		}
	}

	var result []string
	for k := range keys {
		result = append(result, k)
	}
	return result, nil
}
